﻿#include "MiniMapMaskWidget.h"
#include <GraphicsScene.h>
#include <GraphicsView.h>
#include <QPainter>
#include <QMouseEvent>

MiniMapMaskWidget::MiniMapMaskWidget(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::MiniMapMaskWidgetClass())
{
	ui->setupUi(this);
	//setAttribute(Qt::WA_TranslucentBackground);
	//setStyleSheet("background: transparent;"); // 设置背景透明
}

MiniMapMaskWidget::~MiniMapMaskWidget()
{
	delete ui;
}

void MiniMapMaskWidget::setVisableRect(const QRect& rect)
{
	adjustRect(rect);
	update();
}

void MiniMapMaskWidget::setScene(GraphicsScene* scene)
{
	m_scene = scene;
}

QImage MiniMapMaskWidget::renderImage()
{
	// 绘制mask遮罩
	QImage image(size(), QImage::Format_RGBA8888);
	image.fill(Qt::transparent);
	QPainter painter(&image);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	painter.setPen(Qt::NoPen);
	painter.setBrush(QColor(0, 0, 0, 102));
	painter.drawRect(rect());

	// 透明背景
	painter.setCompositionMode(QPainter::CompositionMode_Clear);
	painter.drawRect(m_visableRect);

	// 恢复默认绘制模式
	painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
	painter.setPen(QPen(Qt::white, m_penWidth));

	// 绘制边框
	painter.setBrush(Qt::NoBrush);
	painter.drawRect(m_visableRect);
	return image;
}

void MiniMapMaskWidget::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
	painter.setPen(Qt::NoPen);
	QImage image = renderImage();
	painter.drawImage(QPoint(0, 0), image);
}

void MiniMapMaskWidget::mouseMoveEvent(QMouseEvent* event)
{
	if (!m_isPressRect) {
		QWidget::mouseMoveEvent(event);
		return;
	}
	QPoint curPos = event->pos();
	QPoint deltaPoint = curPos - m_pressPoint;

	QPoint newCenter = m_pressRect.center() + deltaPoint;
	m_visableRect.moveCenter(newCenter);
	// 检查左边界
	if (m_visableRect.left() < m_penWidth / 2) {
		m_visableRect.moveLeft(m_penWidth / 2);
	}
	// 检查上边界
	if (m_visableRect.top() < m_penWidth / 2) {
		m_visableRect.moveTop(m_penWidth / 2);
	}
	// 检查右边界
	if (m_visableRect.right() > width() - m_penWidth) {
		m_visableRect.moveRight(width() - m_penWidth);
	}
	// 检查下边界
	if (m_visableRect.bottom() > height() - m_penWidth) {
		m_visableRect.moveBottom(height() - m_penWidth);
	}
	emit sigCenterMove(m_visableRect.center());
	update();
	QWidget::mouseMoveEvent(event);
}

void MiniMapMaskWidget::mousePressEvent(QMouseEvent* event)
{
	m_scene->isEnableItems(false);
	if (!m_visableRect.contains(event->pos())) {
		m_isPressRect = false;
		QWidget::mousePressEvent(event);
		return;
	}
	m_isPressRect = true;
	m_pressPoint = event->pos();
	m_pressRect = m_visableRect;
	QWidget::mousePressEvent(event);
}

void MiniMapMaskWidget::mouseReleaseEvent(QMouseEvent* event)
{
	m_scene->isEnableItems(true);
	QWidget::mouseReleaseEvent(event);
}

void MiniMapMaskWidget::adjustRect(const QRect& rect)
{
	m_visableRect = rect;
	const QPoint center = rect.center();
	const int w = width();
	const int h = height();
	// 检查宽是否越界
	if (m_visableRect.width() >= w) {
		m_visableRect.setWidth(w - 3);
	}
	if (m_visableRect.height() >= h) {
		m_visableRect.setHeight(h - 3);
	}
	m_visableRect.moveCenter(center);
}
